import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_basic/multi_engine/snake_channel.dart';
import 'package:flutter_basic/route_manager.dart';
import 'package:get/get.dart';

class MainApp extends StatelessWidget{
  final String initialRoute;
  MainApp({
    required this.initialRoute
  });

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      initialRoute: initialRoute,
      theme: ThemeData(
          brightness: Brightness.light,
          primaryColor: Colors.red[800]
      ),
      getPages: RouteManager.getPages,
    );
  }
}

class SecondMainApp extends StatelessWidget{
  /// 从所在的activity或viewController 获取初始路由信息
  Future<String> getInitialRoute() async{
    Map<String, dynamic> result = await SnakeChannel().getInitialRouteInfo();
    return result['initialRoute'];
  }
  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
        future: getInitialRoute(),
        builder: (BuildContext context, AsyncSnapshot<String> snapshot){
          if(snapshot.hasData){
            return GetMaterialApp(
              initialRoute: snapshot.data,
              theme: ThemeData(
                  brightness: Brightness.light,
                  primaryColor: Colors.red[800]
              ),
              getPages: RouteManager.getPages,
              navigatorObservers: [
                SnakeNavigatorObserver(initialRoute: snapshot.data ?? '/'),
              ],
            );
          }else{
            return Container(
              height: double.infinity,
              color: Colors.white,
            );
            // return GetMaterialApp(
            //   home: Container(
            //     color: Colors.white,
            //     alignment: Alignment.center,
            //     child: Text(
            //       '默认显示页面，需要异步获取初始页面信息',
            //       textAlign: TextAlign.center,
            //     ),
            //   ),
            // );
          }
        }
    );

  }

}

class SnakeNavigatorObserver extends NavigatorObserver{

  final String initialRoute;
  SnakeNavigatorObserver({required this.initialRoute});
  bool first = true;
  @override
  void didPush(Route route, Route? previousRoute) {

    if(first && route.settings.name == initialRoute){

    }
    super.didPush(route, previousRoute);
  }
}